package src.domain;

import java.io.Serializable;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.JoinColumn;
import javax.persistence.Version;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;



@Entity
public class PostOffice implements Serializable{	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Long id;
	private int version;
	private String name;
	private Set<PostOffice> trustOffices;
	
	public PostOffice(){}
	
	/*public PostOffice(PostOfficeDTO postOfficeDTO){
		this.id=postOfficeDTO.getId();
		this.version=postOfficeDTO.getVersion();
		this.name=postOfficeDTO.getName();
		
		if (postOfficeDTO.getTrustOffices() != null) {
			Set<PostOffice> postOffices = new HashSet<PostOffice>();
			for (PostOfficeDTO postDTO: postOfficeDTO.getTrustOffices())
				postOffices.add(new PostOffice(postDTO));
			this.trustOffices = postOffices;
		}
	}*/
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Long getId() {
		return id;
	}

	
	public void setId(Long id) {
		this.id = id;
	}

	@Version
	public int getVersion() {
		return version;
	}
	
	public void setVersion(int version) {
		this.version = version;
	}


	@Column(length=50,unique=true)
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	@ManyToMany
	@Fetch(FetchMode.JOIN)
	@JoinTable(name="TrustOffices",joinColumns=@JoinColumn(name="idPostOffice1"),inverseJoinColumns=@JoinColumn(name="idPostOffice2"))
	public Set<PostOffice> getTrustOffices() {
		return trustOffices;
	}
	public void setTrustOffices(Set<PostOffice> trustOffices) {
		this.trustOffices = trustOffices;
	}


	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}


	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		PostOffice other = (PostOffice) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
	
	public String toString(){
		return this.name;
	}
	
}
